// pages/authentication/authentication.js
const app = getApp(),util=require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    veriData:{
      name:'',
      idcard:'',
      mobile:'',
      code:'',
    },
    step:1,
    showBox:true,
    names:"",
    shenfen:'',
    unn:'',
    getting:false,
    twoalert:false,
    twoalertText:'',
    alerts:false,
    cardimg:"../../image/idcardFront.png",
    cardimgs:"../../image/idcardBack.png",
    bankimg:'../../image/bank.png',
    veriBtnText:'获取验证码',
    bankCard:'',
    cardType:'',
    array:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  cancel(){
    let that = this
    that.setData({
      alerts:false,
    })
   
  },

  getPhone(e){
    let that = this
    that.setData({
      veriData:{
        mobile:e.detail.value
      }
    })
  },
  getName(e){
    let name = e.detail
    let that = this
    if(name.keyCode>=48 && name.keyCode<=58){
      that.setData({
        alerts:true,
        twoalert:true,
        twoalertText:"不能输入数字",
        names:that.data.unn
      })
      setTimeout(()=>{
        this.setData({
          alerts:false
        })
      },2000);
    }else{
        that.setData({
          unn:name.value
        })
    }
  },
  getCode(){
    
    let that = this, seconds = 60, timer = null , phone =that.data.veriData.mobile;
    let regu=/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if(regu.test(phone)){
      util.post('users/getcode',{phone:phone},function(res){
        console.log(res)
        if(res.data.code == 1){
          timer = setInterval(function () {
            if (seconds > 1) {
              seconds--
              that.setData({
                veriBtnText: '已发送（' + seconds +'S）',
                getting:true
              })
            } else {
              clearInterval(timer)
              that.setData({
                veriBtnText: '重新发送',
                getting:false
              })
            }
          }, 1000)
        }
      })
     
    }else{
      that.setData({
        alerts:true,
        twoalert:true,
        twoalertText:"手机号有误",
      })
      setTimeout(()=>{
        this.setData({
          alerts:false
        })
      },2000);
    }
    
  },
  successBack(e){
    let imgBack = e.detail.image_path
    let that = this
    console.log(e)
    that.setData({
      cardimgs:imgBack,
      showBox:false,
    })
    wx.uploadFile({
      url: app.globalData.url+'upfiles/upimages',
      filePath: imgBack,
      method:'POST',
      formData:{token:app.globalData.userData.token},
      name:'cardimgs',
      header:{
        'content-type': 'application/x-www-form-urlencoded',
      },
      success:(res)=>{
        console.log(res)
        that.setData({
          cardimgs:JSON.parse(res.data).data[0]
        })
      }
    })
  },
  successFront(e){
    let imgFront = e.detail.image_path
    let that = this
    let name = e.detail.name.text
    let id = e.detail.id.text
    console.log(e)
    that.setData({
      cardimg:imgFront,
      showBox:false,
      names:name,
      shenfen:id,
      veriData:{
        name:name,
        idcard:id
      }
    })
    wx.uploadFile({
      url: app.globalData.url+'upfiles/upimages',
      filePath: imgFront,
      method:'POST',
      formData:{token:app.globalData.userData.token},
      name:'cardimg',
      header:{
        'content-type': 'application/x-www-form-urlencoded',
      },
      success:(res)=>{
        console.log(res)
        that.setData({
          cardimg:JSON.parse(res.data).data[0]
        })
      }
    })
  },
  successBank(e){
    let Card = e.detail.image_path
    let bankCard = e.detail.number.text
    let that = this
    this.setData({
      bankCard
    })
    wx.uploadFile({
      url: app.globalData.url+'upfiles/upimages',
      filePath: Card,
      method:'POST',
      formData:{token:app.globalData.userData.token},
      name:'bankCard',
      header:{
        'content-type': 'application/x-www-form-urlencoded',
      },
      success:(res)=>{
        console.log(JSON.parse(res.data))
        that.setData({
          bankimg:JSON.parse(res.data).data[0]
        })
      }
    })
  },
  req(res){
    this.setData({
      alerts:true,
      twoalert:true,
      twoalertText:res,
    })
    setTimeout(()=>{
      this.setData({
        alerts:false
      })
    },2000);
  },
  step(){
    this.setData({
      step:2
    })
  },
  formSubmit1(e){
    let info = e.detail.value
    let that = this
    if(info.name==""){
      that.req("请输入姓名")
      return
    }else if(that.data.cardimg =='../../image/idcardFront.png'){
      that.req("请上传正面身份证")
      return
    }else if(info.name!=that.data.veriData.name){
      that.req("你输入的名字不一致") 
      return
    }else if(info.idcard==""){
      that.req("请输入身份证")
      return
    }else if(that.data.cardimgs =='../../image/idcardBack.png'){
      that.req("请上传背面身份证")
      return
    }else if(info.idcard.length<18){
      that.req("请输入正确的身份证")
      return
    }else if(info.idcard != that.data.veriData.idcard){
      that.req("您输入的身份证不一致")
      return
    }else if(info.mobile==""){
      that.req("请输入手机号")
      return
    }else if(info.code==""){
      that.req("请输入验证码")
      return
    }else{
      this.setData({
        step:2,
        veriData:e.detail.value
      })
    }
    this.setData({
      step:2,
      veriData:e.detail.value
    })
  },

  formSubmit2(e){
    let that = this
    let info = e.detail.value
    let v = that.data.veriData
    v.card =  info.card
    v.cardname = info.cardName
    v.cardtype = info.cardType
    if(info.cardName ==""){
      that.req(" 请输入持卡人")
    }else if(info.card==""){
      that.req("请输入卡号")
    }else if(info.cardType==''){
      that.req("请选择卡类型")
    }else{
      wx.showModal({
        title: '提交审核',
        content: '提交后将以消息形式通知您审核结果\n是否确认提交', 
        cancelText:'取消', 
        confirmText:'提交',
        confirmColor:'#28AA85',
        success:(res=>{
          if(res.confirm){
            that.req("提交成功")
            util.post('users/userCert',{...v,cardimg:that.data.cardimg,cardimgs:that.data.cardimgs},function(res){
                wx.navigateTo({
                  url: '../../pages/shenhe/shenhe',
                })
              })
          }
         
        })
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  toyonghu(){
    wx.navigateTo({
      url: '../../pages/xieyi/xieyi?type=2',
    })
  },
  onReady: function () {

  },
  bindPickerChange(e){
    let index = e.detail.value
    this.setData({
      cardType:this.data.array[index].name
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    console.log(app.globalData)
    if(app.globalData.userInfo == null){
      this.setData({
        alerts:true,
        twoalert:true,
        twoalertText:'您尚未登录',
      })
      setTimeout(()=>{
        this.setData({
          alerts:false
        })
        wx.switchTab({
          url: '/pages/my/my',
        })
      },1500);
    }
    util.post('standard/bankIncome',{},function(res){
      console.log(res)
      that.setData({
        array:res.data.msg
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})